package b3.scene.home.grid
{
	import as3isolib.display.*;
	import as3isolib.geom.*;
	
	import b3.config.*;
	import b3.widget.basic.*;
	import b3.util.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.scene.home.*;
	
	import com.greensock.*;
	import com.greensock.easing.Linear;
	
	import flash.display.Sprite;
	import flash.text.*;

	public class LivingGrid extends IsoSprite
	{
		public var path:Array;
		private var _direction:int;
		private var moveFlag:Boolean;
		private var speakGrid:IsoSprite;
		
		public function LivingGrid(animalId:int, direct:int = 0)
		{
			super(null);
            var GridClass0:Class = ResourceHelper.getInstance().getClassByName("AnimalGrid" + animalId + "_0");
            var animalSprite0:Sprite = new GridClass0();
            var GridClass1:Class = ResourceHelper.getInstance().getClassByName("AnimalGrid" + animalId + "_1");
            var animalSprite1:Sprite = new GridClass1();
            this.sprites = [animalSprite0, animalSprite1];
            this.direction = direct;
		}
		
		public function updateFootPrint():void
		{
			var direct:int;
			var gridX:int = currentGridX;
			var gridY:int = currentGridY;
			for (var i:int = path.length - 1; i >= 0; i++)
			{
				var pathGrid:TownGridConfig = path[i];
				if (gridX > pathGrid.posX)
				{
					direct = 2;
				}
				else if (gridX < pathGrid.posX)
				{
					direct = 0;
				}
				else if (gridY > pathGrid.posY)
				{
					direct = 3;
				}
				else if (gridY < pathGrid.posY)
				{
					direct = 1;
				}
				
				gridX = pathGrid.posX;
				gridY = pathGrid.posY;
			}
		}
		
		public function startMove():void
		{
			if (!moveFlag)
			{
				onMove();	
			}
		}
		
		public function get currentGridX():int
		{
			return x / Town.gridWidth;
		}
		
		public function get currentGridY():int
		{
			return y / Town.gridWidth;
		}
		
		public function get direction():int
		{
			return _direction;	
		}
		
		public function set direction(direct:int):void
		{
			_direction = direct;
			switch (direct)
			{
				case 0:
					this.sprites[0].visible = true;
					this.sprites[1].visible = false;
					this.sprites[0].scaleX = 1;
					this.sprites[1].scaleX = 1;
				break;
				case 1:
					this.sprites[0].visible = true;
					this.sprites[1].visible = false;
					this.sprites[0].scaleX = -1;
					this.sprites[1].scaleX = 1;
				break;
				case 2:
					this.sprites[0].visible = false;
					this.sprites[1].visible = true;
					this.sprites[0].scaleX = 1;
					this.sprites[1].scaleX = -1;
				break;
				default:
					this.sprites[0].visible = false;
					this.sprites[1].visible = true;
					this.sprites[0].scaleX = 1;
					this.sprites[1].scaleX = 1;
			}
		}
		
		private function onStartMove(targetGrid:TownGridConfig):Boolean
		{
			if (targetGrid == null)
				return false;
			var towerGrid:TownGrid = HomeScene.getInstance().getCurrentTown().towerGrids[targetGrid.posX + "_" + targetGrid.posY];
			if (towerGrid != null)
			{
			}
			return true;
		}
		
		private function onMove():void
		{
			moveFlag = false;	
			if (path != null && path.length > 0)
			{
				var targetGrid:TownGridConfig = path.pop();
				if (currentGridX > targetGrid.posX)
				{
					direction = 2;
				}
				else if (currentGridX < targetGrid.posX)
				{
					direction = 0;
				}
				else if (currentGridY > targetGrid.posY)
				{
					direction = 3;
				}
				else if (currentGridY < targetGrid.posY)
				{
					direction = 1;
				}
			}
		}

		public function speak(content:String):void
		{
			if (speakGrid == null)
			{
				var AnimalMsgBgr:Class = ResourceHelper.getInstance().getClassByName("DialogMsgBgrImage");
				var speakMessage:Sprite = new AnimalMsgBgr();
				speakMessage.height = 60;
				speakMessage.scaleX = speakMessage.scaleY;
				speakMessage.x = 0;
				speakMessage.y = -speakMessage.height;
				
				var speakTextContainer:Sprite = new Sprite();
				var speakText:TextField = new TextField();
				speakText.selectable = false; 
	        	speakText.autoSize = TextFieldAutoSize.LEFT;
				speakText.defaultTextFormat = new TextFormat("微软雅黑", 22, 0x000000);
				speakText.width = 170;
				speakText.wordWrap = true;
				speakText.x = 18;
				speakText.y = 2;
				speakText.text = content;
				speakMessage.addChild(speakText);
				
				var town:Town = HomeScene.getInstance().getCurrentTown();
				speakGrid = new IsoSprite();
				var gridWidth:int = Town.gridWidth;
				speakGrid.setSize(gridWidth, gridWidth, gridWidth);
				speakGrid.moveBy(this.x, this.y, this.z + gridWidth);                        
				town.isoScene.addChild(speakGrid);
				speakGrid.sprites = [speakMessage];
	
				TweenLite.delayedCall(3, removeSpeakMessage);
			}
		}
		
		private function removeSpeakMessage():void
		{
			if (speakGrid != null && speakGrid.parent != null)
			{
				speakGrid.parent.removeChild(speakGrid);
				speakGrid = null;
			}
		}
		
		public function flyOut():void
		{
			TweenLite.to(this, 0.5, {x:-40, y:-40, z:100, onComplete:destory})
		}
		
		public function destory():void
		{
			this.parent.removeChild(this);
		}
		
	}
}